﻿// <copyright file="AddInComposablePartDefinition.cs" company="Anatoly Sereda">
// © 2009 Anatoly Sereda
//
// This source is subject to the Microsoft Public License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Anatoly Sereda</author>
// <email>neonnvr@live.com</email>
// <date>2009-08-04</date>

using System;
using System.AddIn.Hosting;
using System.Collections.Generic;
using System.ComponentModel.Composition.Primitives;
using System.Linq;

namespace MafMefInterop
{
	public class AddInComposablePartDefinition : ComposablePartDefinition, ICompositionElement
	{
		public AddInComposablePartDefinition(AddInToken token, Type contractType, AddInCatalog catalog)
		{
			this.AddInToken = token;
			this.ContractType = contractType;
			this.Catalog = catalog;
		}

		public override IEnumerable<ExportDefinition> ExportDefinitions
		{
			get
			{
				AddInExportDefinition export = new AddInExportDefinition(this);

				if (this.Catalog.Validator != null)
				{
					if (!this.Catalog.Validator(export.Metadata))
					{
						yield break;
					}
				}

				yield return new AddInExportDefinition(this);
			}
		}

		public override IEnumerable<ImportDefinition> ImportDefinitions
		{
			get { return Enumerable.Empty<ImportDefinition>(); }
		}

		public Type ContractType { get; private set; }

		public string DisplayName
		{
			get { return null; }
		}

		public ICompositionElement Origin
		{
			get { return this.Catalog; }
		}

		internal AddInCatalog Catalog { get; private set; }

		internal AddInToken AddInToken { get; private set; }

		public override ComposablePart CreatePart()
		{
			return new AddInComposablePart(this);
		}
	}
}
